package interviewpreparationkit.sorting

/**
 * [Sorting: Bubble Sort](https://www.hackerrank.com/challenges/ctci-bubble-sort)
 */
// Complete the countSwaps function below.
fun countSwaps(a: Array<Int>): Unit {
    val n = a.size
    var t: Int
    var swapped = 0
    for (i in 0 until n) {

        for (j in 0 until n - 1) {
            // Swap adjacent elements if they are in decreasing order
            if (a[j] > a[j + 1]) {
                swapped++
                t = a[j]
                a[j] = a[j + 1]
                a[j + 1] = t
            }
        }

    }

    println(
        "Array is sorted in $swapped swaps.\n" +
                "First Element: ${a[0]}\n" +
                "Last Element: ${a.last()}"
    )
}